/***
 * This example expects the serial port has a loopback on it.
 *
 * Alternatively, you could use an Arduino:
 *
 * <pre>
 *  void setup() {
 *    Serial.begin(<insert your baudrate here>);
 *  }
 *
 *  void loop() {
 *    if (Serial.available()) {
 *      Serial.write(Serial.read());
 *    }
 *  }
 * </pre>
 */

#include <string>
#include <iostream>
#include <cstdio>

 // OS Specific sleep
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

#include "serial/serial.h"

using std::string;
using std::exception;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;

void my_sleep(unsigned long milliseconds) {
#ifdef _WIN32
	Sleep(milliseconds); // 100 ms
#else
	usleep(milliseconds * 1000); // 100 ms
#endif
}

void enumerate_ports()
{
	vector<serial::PortInfo> devices_found = serial::list_ports();

	vector<serial::PortInfo>::iterator iter = devices_found.begin();

	while (iter != devices_found.end())
	{
		serial::PortInfo device = *iter++;

		printf("(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
			device.hardware_id.c_str());
	}
}

void print_usage()
{
	cerr << "用法: show {-e|<串口地址>} ";
	cerr << "<波特率> [测试字符串]" << endl;
}

int run(int argc, char** argv)
{
	if (argc < 2) {
		print_usage();
		return 0;
	}

	// Argument 1 is the serial port or enumerate flag
	string port(argv[1]);

	if (port == "-e") {
		enumerate_ports();
		return 0;
	}
	else if (argc < 3) {
		print_usage();
		return 1;
	}

	// Argument 2 is the baudrate
	unsigned long baud = 0;
#if defined(WIN32) && !defined(__MINGW32__)
	sscanf_s(argv[2], "%lu", &baud);
#else
	sscanf(argv[2], "%lu", &baud);
#endif

	// port, baudrate, timeout in milliseconds
	serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(50000));

	cout << "串口";
	if (my_serial.isOpen())
		cout << "已开启." << endl;
	else
		cout << "未开启." << endl;

	// Get the Test string
	int count = 0;
	string test_string;
	if (argc == 4) {
		test_string = argv[3];
	}
	else {
		test_string = "测试";
	}

	// Test the timeout, there should be 1 second between prints
	cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
	cout<<"试读20次"<<endl;
	while (count < 20) {
		//size_t bytes_wrote = my_serial.write(test_string);

		string result = my_serial.readline();

		cout << "Iteration: " << count << ", Bytes written: ";
		//cout << bytes_wrote << ", Bytes read: ";
		cout << result.length() << ", 读到: " << result << endl;

		count += 1;
	}
	return 0;
}

int main(int argc, char** argv) {
	try {
		return run(argc, argv);
	}
	catch (exception& e) {
		cerr << "Unhandled Exception: " << e.what() << endl;
	}
}
